package edu.uit.se.laps.web.controller;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.classic.Session;

import edu.uit.se.laps.web.model.LoaiSanPham;
import edu.uit.se.laps.web.model.TaiKhoan;
import edu.uit.se.laps.web.util.HibernateUtil;

public class LoaiSanPhamManager extends HibernateUtil{
	
	public LoaiSanPham addLoaiSanPham(LoaiSanPham loaisanpham) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.save(loaisanpham);
        session.getTransaction().commit();
        return loaisanpham;
    }
	
    public LoaiSanPham deleteLoaiSanPham(int id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        //SanPham sanpham = (SanPham) session.load(SanPham.class, id);
        LoaiSanPham loaisanpham = (LoaiSanPham) session.get(LoaiSanPham.class, id);
        if(null != loaisanpham) {
            session.delete(loaisanpham);
        }
        session.getTransaction().commit();
        return loaisanpham;
    }
 
    public List<LoaiSanPham> list() {
         
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<LoaiSanPham> loaisanpham = null;
        try {
             
            loaisanpham = (List<LoaiSanPham>)session.createQuery("from LoaiSanPham").list();
             
        } catch (HibernateException e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }
        session.getTransaction().commit();
        return loaisanpham;
    }
    
    public boolean updateLoaiSanPham(LoaiSanPham loaisanpham){
		boolean result = false;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		try {
			session.update(loaisanpham);
			result = true;
		} catch (Exception e) {
			session.getTransaction().rollback();
		}		
		session.getTransaction().commit();
		return result;
	}
    
    public LoaiSanPham getLoaiSanPham(int id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        
        
        LoaiSanPham loaisanpham = (LoaiSanPham) session.get(LoaiSanPham.class, id);
        if (null != loaisanpham) {
        	session.getTransaction().commit();
            return loaisanpham;
		} else {
			session.getTransaction().rollback();
	        return null;

		}
        
    }
    
    public LoaiSanPham getLoaiSP(String tenLoaiSP){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		try {
			String sql = "from LoaiSanPham where tenLoaiSP = ?";
			Query query = session.createQuery(sql);
			query.setString(0, tenLoaiSP);
			
			
			LoaiSanPham loaiSp = (LoaiSanPham)query.uniqueResult();
			session.getTransaction().commit();
			
			return loaiSp;
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
			System.out.println("Loi lay loai san pham");
		}
			
		return null;
	}
  
    
}
